The present invention relates in general to data processing systems, and in particular, to a method for initializing variables within classes in an object-oriented programming environment.
Object-oriented programming implements programming techniques in an attempt to model real-world objects. An object is a software xe2x80x9cpackagexe2x80x9d that contains a collection of related procedures and data. Such procedures are called methods, and the data elements are referred to as variables. Objects interact with each other by sending each other messages requesting that they carry out their methods. A message is simply the name of an object followed by the name of a method the object knows how to execute. Therefore, object-oriented programming consists of a number of objects interacting with each other by sending messages to one another.
A class is a template that defines the methods and variables to be included in a particular type of object. The descriptions of the methods and variables that support them are included only once, in the definition of the class. The objects that belong to a class, called instances of the class, contain only their particular values for the variables. FIG. 1 illustrates a class 100 having methods 101-105 and N variables.
Therefore, in an object-oriented programming system, objects are grouped into classes. The classes define the fields and methods that can be applied to an object. Object classes are organized into an inheritance hierarchy which defines how some object classes can inherit fields and methods and extend them with additional fields and methods. In addition to the inheritance hierarchy, classes and methods may use other classes and methods in their implementation.
Classes and objects may have fields which need to be initialized to the proper initial values. Object instance fields are initialized when an object is created. Class fields must be initialized before any object of the class is created or used. Classes must be initialized in a correct order. Many orders may be possible, but there are a set of constraints defined by the definition of the class which can be determined either by the programmer or at compile time.
Some languages, such as JAVA, dynamically load classes when they are first referenced and initialize the class when the class is dynamically loaded. For example, when the JAVA program gets loaded into memory, it is at that point that the variables are initialized. Some of the variables within a class may be already determined, but there may be some variables that are defined by some function, such as the square root of 2, which are performed by methods within other classes. To determine that variable, there is a call to that method (square root of 2 function) within another class object. The JAVA program will then have to load that class object into memory, and initialize its core variables.
In JAVA this is fairly straight forward because of the dynamic loading of the JAVA program. Other languages, such as C++, statically load classes and provide little to control the order of class initialization.
In a C++ program, the above method would require that all of the classes be loaded at the same time. One of the problems is that the order for initialization within C++ is not defined. A programmer can attempt to define a set of priorities for ordering the initializations, but if a circularity is encountered, there can be a difficulty in dealing with such a situation.
The present invention addresses the foregoing problems by defining a variable for each class indicating whether the initialization of the core values in that class have completed or are in progress, and by also incorporating an xe2x80x9cimportance levelxe2x80x9d parameter to each class initialization method. The importance level parameter ensures that each class initialization method initializes its BEFORE classes first, and then initializes the AFTER classes at a later time (BEFORE and AFTER are defined hereinafter). This allows the process to first initialize all important classes (where the definition of what is important is determined locally by the class and the situation), but still ensures that all reachable classes are initialized eventually before beginning the main method.
In one embodiment of the present invention a program method is implemented for initializing variables in each of a plurality of class objects created in a statically loaded object-oriented programming language. Within each of the class objects, messages are sent to BEFORE class objects to initialize their core variables. Also, within each of the class objects, initializing of the core variables is performed after all of the BEFORE class objects have initialized their core variables. BEFORE class objects are those class objects listed whose core variables need to be initialized before the core variables of the present class object. Within each of the class objects after the core variables have been initialized, AFTER classes may be initialized.
The foregoing has outlined rather broadly the features and technical advantages of the present invention in order that the detailed description of the invention that follows may be better understood. Additional features and advantages of the invention will be described hereinafter which form the subject of the claims of the invention.